
import Vue from 'vue'
let boxSize = {
  width: 500,
  height: 76,
  offset: 15
}
export let drawLine = (lineData) => {
  lineData.forEach(element => {
    element.position = [element.x, element.y]
  })
  let arr = warpCanvasLink(lineData)
  for (var i = 0; i < arr.length - 1; i++) {
    let element = arr[i]
    for (var j = 0; j < arr.length - 1 - i; j++) {
      let ele = arr[j]
      if (!(element.canvasNode[0].id === ele.canvasNode[0].id && element.canvasNode[1].id === ele.canvasNode[1].id)) {
        if (element.canvasNode[1].pid === ele.canvasNode[1].pid && ele.canvas[0][0] === ele.canvas[1][0]) {
          ele.arc = true
        }
      }
    }
  }
  return arr
}

let warpCanvasLink = (lineGp) => {
  let linkPointGp = []
  for (var i = 0; i < lineGp.length; i++) {
    for (var j = 0; j < lineGp.length; j++) {
      if (lineGp[j].pid === lineGp[i].id) {
        linkPointGp.push(calculateDirecte(lineGp[i], lineGp[j]))
      }
    }
  }
  return linkPointGp
}
let calculateDirecte = (start, end) => {
  let formPoint, frirstBrokenPoint, secondBrokenPoint, toPoint, spaceX, spaceY
  let startPoint = start.position
  let endPoint = end.position
  spaceX = Math.abs(startPoint[0] - endPoint[0])
  spaceY = Math.abs(startPoint[1] - endPoint[1]) - boxSize.height
  if (startPoint[0] > endPoint[0]) {
    formPoint = [spaceX + boxSize.offset, 0]
    frirstBrokenPoint = [spaceX + boxSize.offset, (spaceY / 2)]
    secondBrokenPoint = [0 + boxSize.offset, (spaceY / 2)]
    toPoint = [0 + boxSize.offset, spaceY]
  } else if (startPoint[0] < endPoint[0]) {
    formPoint = [0 + boxSize.offset, 0]
    frirstBrokenPoint = [0 + boxSize.offset, spaceY / 2]
    secondBrokenPoint = [spaceX + boxSize.offset, spaceY / 2]
    toPoint = [spaceX + boxSize.offset, spaceY]
  } else {
    formPoint = [0 + boxSize.offset, 0]
    // frirstBrokenPoint = [0 + boxSize.offset, spaceY / 2]
    frirstBrokenPoint = [0 + boxSize.offset, spaceY]
    // secondBrokenPoint = [0 + boxSize.offset, spaceY / 2]
    secondBrokenPoint = [0 + boxSize.offset, spaceY]
    toPoint = [spaceX + boxSize.offset, spaceY]
  }
  return {
    canvas: [start.position, end.position],
    line: [formPoint, frirstBrokenPoint, secondBrokenPoint, toPoint],
    canvasNode: [start, end]
  }
}

export const Component = new Vue()
